package com.yami.shop.mystery.common.listener;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.event.MysteryBoxSoldOutEvent;
import com.yami.shop.bean.event.PaySuccessOrderEvent;
import com.yami.shop.bean.order.PaySuccessOrderOrder;
import com.yami.shop.mystery.common.dao.MysteryBoxOrderMapper;
import com.yami.shop.mystery.common.dao.MysteryBoxUserArkMapper;
import com.yami.shop.mystery.common.model.MysteryBoxOrder;
import com.yami.shop.mystery.common.model.MysteryBoxUserArk;
import com.yami.shop.mystery.common.service.MysteryBoxOpenRecordService;
import com.yami.shop.mystery.common.service.MysteryBoxUserArkService;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 盲盒成功回调
 * @author wxf
 * @date 2023/4/13 11:44
 */
@Component("mysteryBoxPaySuccessListener")
@AllArgsConstructor
public class PaySuccessOrderListener {

    private final MysteryBoxOrderMapper mysteryBoxOrderMapper;

    private final MysteryBoxUserArkMapper mysteryBoxUserArkMapper;

    private final MysteryBoxUserArkService mysteryBoxUserArkService;

    private final MysteryBoxOpenRecordService mysteryBoxOpenRecordService;

    private final ApplicationEventPublisher eventPublisher;

    /**
     * 更新盲盒状态
     */
    @EventListener(PaySuccessOrderEvent.class)
    @Order(PaySuccessOrderOrder.MYSTERYBOX)
    public void mysteryBoxPaySuccessListener(PaySuccessOrderEvent event) {
        List<com.yami.shop.bean.model.Order> orders = event.getOrders();

        for (com.yami.shop.bean.model.Order order : orders) {
            if (!Objects.equals(order.getOrderMold(), 3)) {
                return;
            }
            // 更新盲盒状态
            mysteryBoxOrderMapper.updateStateByOrderNumber(order.getOrderNumber(), 1);
            // 更新用户持仓盲盒数
            MysteryBoxOrder mysteryBoxOrder = mysteryBoxOrderMapper.selectOne(Wrappers.lambdaQuery(MysteryBoxOrder.class)
                    .eq(MysteryBoxOrder::getOrderNumber, order.getOrderNumber()));

            // 先初始化盲盒，初始化失败，存入用户盒柜，由定时任务来处理
            mysteryBoxOpenRecordService.initMysteryBoxOpenRecord(mysteryBoxOrder);
            MysteryBoxUserArk mysteryBoxUserArk = mysteryBoxUserArkMapper.selectOne(Wrappers.lambdaQuery(MysteryBoxUserArk.class)
                    .eq(MysteryBoxUserArk::getUid, mysteryBoxOrder.getUserId()).eq(MysteryBoxUserArk::getBid, mysteryBoxOrder.getMysteryBoxId()));
            if (ObjectUtil.isEmpty(mysteryBoxUserArk)) {
                MysteryBoxUserArk userArk = new MysteryBoxUserArk();
                userArk.setUid(mysteryBoxOrder.getUserId());
                userArk.setBid(mysteryBoxOrder.getMysteryBoxId());
                userArk.setCount(mysteryBoxOrder.getProdCount());
                userArk.setStatus("0");
                mysteryBoxUserArkMapper.insert(userArk);
            } else {
                mysteryBoxUserArkService.update(Wrappers.lambdaUpdate(MysteryBoxUserArk.class)
                        .setSql("count = count + " + mysteryBoxOrder.getProdCount() + ",status = 0")
                        .eq(MysteryBoxUserArk::getUid, mysteryBoxOrder.getUserId()).eq(MysteryBoxUserArk::getBid, mysteryBoxOrder.getMysteryBoxId()));
            }

            // 锁库存为0，库存为0，修改盲盒为售罄
            MysteryBoxSoldOutEvent soldOutEvent = new MysteryBoxSoldOutEvent(mysteryBoxOrder.getMysteryBoxId());
            eventPublisher.publishEvent(soldOutEvent);
        }

    }

}
